import {Sprite} from "../base/Sprite.js";
import {DataStore} from "../base/DataStore.js";

export class Bird extends Sprite {
    constructor() {
        const image = DataStore.getInstance().resLoaded.get('birds');
        super(image, 0, 0, image.width, image.height, 0, 0, image.width, image.height);
        this.step = 0;
        this.time = 0;
        this.downtime = 0;

        this.x = DataStore.getInstance().canvas.width / 2;
        this.y = DataStore.getInstance().canvas.height / 2;
        this.curY = 0;
        this.offsetUp = 0;


        this.clippingX = [
            9,
            9 + 34 + 18,
            9 + 34 + 18 + 34 + 18
        ];

        this.clippingY = [
            10, 10, 10
        ];

        this.clippingWidth = [34, 34, 34];
        this.clippingHeight = [24, 24, 24]

    }

    draw(img = this.img, srcX = this.srcX, srcY = this.srcY, srcW = this.srcW, srcH = this.srcH, x = this.x, y = this.y, width = this.width, height = this.height) {
        this.time = ++this.time % 60;
        this.downtime++;
        if (this.time === 0) {
            this.step = 0;
        } else if (this.time === 20) {
            this.step = 1;
        } else if (this.time === 40) {
            this.step = 2;
        }

        this.srcX = this.clippingX[this.step];
        this.srcY = this.clippingY[this.step];
        this.srcW = this.clippingWidth[this.step];
        this.srcH = this.clippingHeight[this.step];


        const g = 0.98 / 2.2;
        this.offsetUp = 35;
        const offsetY = (g * this.downtime * (this.downtime - this.offsetUp)) / 2;

        this.curY = y + offsetY;

        super.draw(img, srcX, srcY, srcW, srcH, x, y + offsetY, srcW, srcH);
    }
}